﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using QuickUIT.Utilities;

namespace QuickUIT.Controls
{
    /// <summary>
    /// Gets a list type control. 
    /// </summary>
    public class UIList : UIBaseControl
    {
        /// <summary>
        /// Gets the number of grid rows. 
        /// </summary>
        public new int RowCount
        {
            get { return base.RowCount; }
        }

        /// <summary>
        /// Gets the number of grid columns.  
        /// </summary>
        public new int ColumnCount
        {
            get { return base.ColumnCount; }
        }

        /// <summary>
        /// Gets a value which indicates whether the control can scroll horizontally. 
        /// </summary>
        public new bool HorizontallyScrollable
        {
            get { return base.HorizontallyScrollable; }
        }

        /// <summary>
        /// Gets a value which indicates whether the control can scroll horizontally. 
        /// </summary>
        public new bool VerticallyScrollable
        {
            get { return base.VerticallyScrollable; }
        }

        /// <summary>
        /// Gets the value of the horizontal scroll position.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN. 
        /// </summary>
        public new double HorizontalScrollPercent
        {
            get { return base.HorizontalScrollPercent; }
        }

        /// <summary>
        /// Gets the value of the vertical scroll position 
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double VerticalScrollPercent
        {
            get { return base.VerticalScrollPercent; }
        }

        /// <summary>
        /// Gets the value of the horizontal size of the viewable region.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double HorizontalViewSize
        {
            get { return base.HorizontalViewSize; }
        }

        /// <summary>
        /// Gets the value of the vertical size of the viewable region.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double VerticalViewSize
        {
            get { return base.VerticalViewSize; }
        }

        /// <summary>
        /// Gets the current control specific view. 
        /// </summary>
        public new int CurrentView
        {
            get { return base.CurrentView; }
        }

        /// <summary>
        /// Gets a value that indicates whether the control requires at least one child child element to be selected. 
        /// </summary>
        public new bool IsSelectionRequired
        {
            get { return base.IsSelectionRequired; }
        }

        /// <summary>
        /// Gets a value that indicates whether the control allows more than one child element to be selected concurrently. 
        /// </summary>
        public new bool CanSelectMultiple
        {
            get { return base.CanSelectMultiple; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class. 
        /// </summary>
        public UIList() : base(ControlType.List)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        public UIList(UIBaseControl parent) : base(parent, ControlType.List)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class.
        /// </summary>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIList(string locator) : base(null, ControlType.List, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIList(UIBaseControl parent, string locator) : base(parent, ControlType.List, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class.
        /// </summary>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIList(AutomationElement control) : base(control, ControlType.List)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIList"/> class.
        /// </summary>
        /// <param name="parent">The parent of the control</param>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIList(UIBaseControl parent, AutomationElement control) : base(parent, control, ControlType.List)
        {
        }

        /// <summary>
        /// Scrolls the visible region of the content area horizontally and vertically. 
        /// </summary>
        /// <param name="horizontalAmount">The horizontal scrolling increment that is specific to the control.</param>
        /// <param name="verticalAmount">The vertical scrolling increment that is specific to the control.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        /// <remarks>No scroll should be passed if if the control cannot be scrolled in a given direction.</remarks>
        public new void Scroll(ScrollAmount horizontalAmount, ScrollAmount verticalAmount)
        {
            base.Scroll(horizontalAmount, verticalAmount);
        }

        /// <summary>
        /// Sets the horizontal and/or vertical scroll of the document.  
        /// </summary>
        /// <param name="horizontalPercent">
        /// The percentage of the total horizontal content area. 
        /// NoScroll should be passed in if the control cannot be scrolled in this direction.
        /// </param>
        /// <param name="verticalPercent">
        /// The percentage of the total vertical content area. 
        /// NoScroll should be passed in if the control cannot be scrolled in this direction.
        /// </param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new void SetScrollPercent(double horizontalPercent, double verticalPercent)
        {
            base.SetScrollPercent(horizontalPercent, verticalPercent);
        }

        /// <summary>
        /// Scroll the list vertically by the a <see cref="ScrollAmount"/>.
        /// </summary>
        /// <param name="amount">The amount to scroll by.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new void ScrollVertically(ScrollAmount amount)
        {
            base.ScrollVertically(amount);
        }

        /// <summary>
        /// Scroll the list horizontally by the a <see cref="ScrollAmount"/>.
        /// </summary>
        /// <param name="amount">The amount to scroll by.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new void ScrollHorizontally(ScrollAmount amount)
        {
            base.ScrollHorizontally(amount);
        }

        /// <summary>
        /// Scrolls to the top of the list control. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new void ScrollToTop()
        {
            base.ScrollToTop();
        }

        /// <summary>
        /// Selects an item from the combo box by name. 
        /// </summary>
        /// <param name="itemName">The name of the item to select from the combo box.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public void SelectItem(string itemName)
        {
            var criteria = new AutomationElementSearchCriteria();
            criteria.Add(new AutomationElementExactSearchCondition(AutomationElementIdentifiers.NameProperty, itemName));
            AutomationElement item = AutomationElementFinder.FindFirst(Control, criteria);
            while (item == null && !double.IsNaN(VerticalScrollPercent) && VerticalScrollPercent != ScrollPattern.NoScroll && VerticalScrollPercent != 100.0)
            {
                ScrollVertically(ScrollAmount.LargeIncrement);
                item = AutomationElementFinder.FindFirst(Control, criteria);
            }
            AutomationElementPatterns.ScrollIntoView(item);
            AutomationElementPatterns.Select(item);
        }

        /// <summary>
        /// Gets a collection of automation elements that represents the current selection for an element.
        /// </summary>
        /// <returns>A collection of selected elements.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new AutomationElement[] GetSelection()
        {
            return base.GetSelection();
        }

        /// <summary>
        /// Gets the current selection for this list control.
        /// </summary>
        /// <returns>Automation element of the current selected list item. Nothing if there is no selected item.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public AutomationElement GetSelectedItem()
        {
            WaitForParentWindowToBeReady();
            var selection = AutomationElementPatterns.GetSelection(Control);
            return selection.Length == 0 ? null : selection[0];
        }

        /// <summary>
        /// Retrieves a collection of integer values that identify the views available for a control.
        /// </summary>
        /// <returns>A collection of view identifiers. An empty collection is returned if there are no found view identifiers.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element does is not available anymore.</exception>
        public new int[] GetSupportedViews()
        {
            return base.GetSupportedViews();
        }

        /// <summary>
        /// Sets the control specific view. 
        /// </summary>
        /// <param name="viewId">The view identifier to set.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        public new void SetCurrentView(int viewId)
        {
            base.SetCurrentView(viewId);
        }

        /// <summary>
        /// Retrieves an element that represents the specified cell in a grid control. 
        /// </summary>
        /// <param name="row">The ordinal number of the row of interest.</param>
        /// <param name="col">The ordinal number of the column of interest.</param>
        /// <returns>The <see cref="AutomationElement"/> that represents the retrieved cell.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The current element does not exist anymore.</exception>
        /// <remarks>Grid coordinates are zero-based with the upper left (or upper right cell depending on locale) having coordinates (0,0).</remarks>
        public new AutomationElement GetItem(int row, int col)
        {
            return base.GetItem(row, col); 
        }
    }
}
